package cn.nexttec.noteplayer.databases.converters

import android.annotation.SuppressLint
import androidx.room.TypeConverter
import java.text.SimpleDateFormat
import java.util.*

class DateConverter {

    companion object {
        @SuppressLint("SimpleDateFormat")
        private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    }

    @TypeConverter
    fun toDateText(date: Date?) : String {
        if (date == null) return ""
        return dateFormat.format(date)
    }

    @TypeConverter
    fun toDate(value: String?) : Date? {
        if (value != null) {
            try {
//                Log.d("STTool", "$value 转换 Date")
                if (value.isNotEmpty()) {
                    return dateFormat.parse(value)
                }
            } catch (ex: Throwable) {
                ex.printStackTrace()
            }
        }
        return null
    }

}